/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.rpc.http;

import org.openislands.oi.rpc.base.Executor;
import org.openislands.oi.rpc.base.ExecutorProxy;
import org.openislands.oi.rpc.base.Extended;
import org.openislands.oi.rpc.base.ItemExtended;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class HttpExecutorProxy implements ExecutorProxy {
    private static final Logger log = LoggerFactory.getLogger(HttpExecutorProxy.class);

    /***
     * Controller proxy cache
     * <Key>        Controller.class
     * <Value>      cglib create proxy instance
     */
    private final Map<Class<?>, Object> proxyObjectCache = new ConcurrentHashMap<>();

    @SuppressWarnings("unused")
    @Autowired
    private HttpRequestInterceptor httpRequestInterceptor;

    /***
     * get the proxy object executor
     * @param proxyClass class
     * @return proxy Executor
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> Executor<T> getExecutor(Class<T> proxyClass) {
        Object proxyObject = this.proxyObjectCache.get(proxyClass);
        final HttpExecutor<T> httpExecutor = new HttpExecutor<>(proxyClass);
        // set executor into thread local
        this.httpRequestInterceptor.setExecutor(httpExecutor);
        if (Objects.isNull(proxyObject)) {
            Enhancer enhancer = new Enhancer();
            // set superclass of the proxy class
            enhancer.setSuperclass(proxyClass);
            // set method invoke interceptor
            enhancer.setCallback(this.httpRequestInterceptor);
            // create proxy class instance
            proxyObject = enhancer.create();
            this.proxyObjectCache.put(proxyClass, proxyObject);
            log.info("put controller: {} proxy object: {}", proxyClass.getName(), proxyObject);
        }
        httpExecutor.setProxy((T) proxyObject);
        return httpExecutor;
    }

    public static class HttpExecutor<T> implements Executor<T> {
        private final Extended<Class<?>, ItemExtended> extended = new Extended<Class<?>, ItemExtended>() {
        };
        private final Class<T> source;
        private T proxy;

        public HttpExecutor(Class<T> source) {
            this.source = source;
        }

        public void setProxy(T proxy) {
            this.proxy = proxy;
        }

        public Class<T> getSource() {
            return this.source;
        }

        @Override
        public T executor() {
            return this.proxy;
        }

        @Override
        public Extended<Class<?>, ItemExtended> extended() {
            return this.extended;
        }
    }
}
